//
//  NowViewController.m
//  CKApp
//
//  Created by CC on 2020/8/26.
//  Copyright © 2020 This Moment. All rights reserved.
//

#import "NowViewController.h"
#import "LcFoundation.h"
#import "CCCollectionAdapter.h"
#import "Masonry.h"
#import "FeedManager.h"
#import "MessageDetailViewController.h"
#import "PostMessageViewController.h"
#import "CCNavigationController.h"

@interface NowViewController () <UIScrollViewDelegate>
@property(nonatomic, strong) IBOutletCollection(UIButton) NSArray<UIButton*>* categoryButtons;
@property(nonatomic, weak) IBOutlet UIButton* postMessageButton;
@property(nonatomic, strong) UIScrollView* scrollView;
@property(nonatomic, strong) NSMutableArray* collectionViews;
@property(nonatomic, strong) NSMutableArray* cellAdapters;
@property(nonatomic, assign) MessageSource source;
@end

@implementation NowViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  [self setUp];
}

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  [self.navigationController setNavigationBarHidden:YES animated:NO];
}

- (void)viewWillDisappear:(BOOL)animated {
  [super viewWillDisappear:animated];
  [self.navigationController setNavigationBarHidden:NO animated:NO];
}

- (void)viewDidLayoutSubviews {
  [super viewDidLayoutSubviews];
  if (self.collectionViews.count > 0) {
    for (UICollectionView* collectionView in self.collectionViews) {
      collectionView.frame = CGRectMake(collectionView.posX, collectionView.posY, collectionView.width, self.scrollView.height);
    }
  }
}

- (void)setUp {
  self.collectionViews = [[NSMutableArray alloc] initWithCapacity:self.categoryButtons.count];
  self.cellAdapters = [[NSMutableArray alloc] initWithCapacity:self.categoryButtons.count];

  self.scrollView = [[UIScrollView alloc] init];
  self.scrollView.showsHorizontalScrollIndicator = YES;
  self.scrollView.pagingEnabled = YES;
  self.scrollView.delegate = self;
  [self.scrollView
      setContentSize:CGSizeMake(LcUIUtility.screenWidth * self.categoryButtons.count, 0)];
  [self.view addSubview:self.scrollView];
  LcWeakSelf;
  [self.scrollView mas_makeConstraints:^(MASConstraintMaker* make) {
    LcStrongSelf;
    make.top.equalTo(self.topGuideBackgroundView.mas_bottom);
    make.left.right.bottom.equalTo(self.view);
  }];

  for (NSInteger i = 0; i < self.categoryButtons.count; i++) {
    UICollectionView* collectionView = [[UICollectionView alloc]
               initWithFrame:CGRectMake(LcUIUtility.screenWidth * i, 0, LcUIUtility.screenWidth,
                                        self.scrollView.height)
        collectionViewLayout:[UICollectionViewFlowLayout new]];
    [self.scrollView addSubview:collectionView];
    [self.collectionViews addObject:collectionView];
    collectionView.backgroundColor = [UIColor colorWithRed:(arc4random_uniform(256)) / 255.0
                                                     green:(arc4random_uniform(256)) / 255.0
                                                      blue:(arc4random_uniform(256)) / 255.0
                                                     alpha:1.0];
    CCCollectionAdapter* cellAdapter = [[CCCollectionAdapter alloc] init];
    [cellAdapter registerCollectionView:collectionView
                        scrollDirection:UICollectionViewScrollDirectionVertical];
    [self.cellAdapters addObject:cellAdapter];
  }

  [self reloadCategoryButtons];
  [self.view bringSubviewToFront:self.postMessageButton];
}

#pragma mark - Refresh
- (void)refreshAll:(BOOL)force {
  if (self.source == MESSAGE_SOURCE_RECOMMENDATION) {

  } else if (self.source == MESSAGE_SOURCE_ATTENTION) {

  } else if (self.source == MESSAGE_SOURCE_LIVE) {

  } else {
    LC_FAIL();
  }
}

- (void)switchContentWithSource:(MessageSource)source {
  self.source = source;
  [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
    [self reloadCategoryButtons];
  } completion:nil];
}

- (void)reloadCategoryButtons {
  for (UIButton* button in self.categoryButtons) {
    if (button.tag == self.source) {
      [button setLcFont:[LcFont db24]];
    } else {
      [button setLcFont:[LcFont tgb18]];
    }
  }
}

#pragma mark - RequestData
- (void)requestData {
}

#pragma mark - Action
- (IBAction)buttonClicked:(UIButton*)sender {
  MessageSource source = (MessageSource)[sender tag];
  [self switchContentWithSource:source];
  [self.scrollView setContentOffset:CGPointMake(LcUIUtility.screenWidth * source, 0) animated:YES];
}

- (IBAction)postClicked:(id)sender {
  PostMessageViewController* viewCtrl = [[PostMessageViewController alloc] init];
  CCNavigationController* nav = [[CCNavigationController alloc] initWithRootViewController:viewCtrl];
  nav.modalPresentationStyle = UIModalPresentationFullScreen;
  [self.navigationController presentViewController:nav animated:YES completion:nil];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  CGPoint contentOffSet = [scrollView contentOffset];
  MessageSource source = (MessageSource)(contentOffSet.x)/LcUIUtility.screenWidth;
  [self switchContentWithSource:source];
}

@end
